/**
 * <p>文件上传控件, 可以支持用户上传多个文件,但一次只能上传一个文件</p>
 * 
 * @class 文件上传类
 * @param {string} action 处理文件上传的jsp或者servlet的url
 * @param {number} [count] 初始显示的文件上传的个数, 默认为1
 * @param {boolean} [allowAdd] 是否允许添加, 默认为不能
 * @param {boolean} [showAddbtn] 是否显示默认的添加按钮, 默认为不显示
 * @param {boolean} [showUploadbtn] 是否显示默认的上传按钮, 默认为不显示
 */
zxt.ui.Upload = function(action,count,allowAdd,showAddbtn,showUploadbtn) {
	zxt.ui.Widget.call(this);
	var instance = this;
	this._count = Number(count)||1;
	this._action = action;
	this._$uploadiframe = $("<iframe name=\"uploadframe\" frameBorder=\"0\" style=\"display:none\"></iframe>");
	this._$form = $("<FORM name=\"uploadForm\" METHOD=\"POST\" ACTION=\""+action+"\" ENCTYPE=\"multipart/form-data\" target=\"uploadframe\"></FORM>");
	this._allowAdd = allowAdd||false;
	this._showAddbtn = showAddbtn||false;
	this._showUploadbtn = showUploadbtn||false;
	if(this._showUploadbtn){
		this._$submit = $("<input type=\"button\" name=\"Submit\" value=\"上传它！\">");
		this._$submit.click(function(){
			instance.doUpload();
		});
	};
	this._$jqDom = $("<div nowrap>");
	
};

/**
 * <p>根据传入的DOM元素, 解析传入的相关属性, 创建相应的文件上传控件对象</p>
 * 
 * @function
 * @static
 * @param {DOM} elem 解析的DOM元素
 * @param {DOM} zxt.action 处理文件上传的jsp或者servlet的url
 * @param {DOM} [zxt.count] 初始显示的文件上传的个数, 默认为1
 * @param {DOM} [zxt.allowAdd] 是否允许添加, 默认为不能
 * @param {DOM} [zxt.showAddbtn] 是否显示默认的添加按钮, 默认为不显示
 * @param {DOM} [zxt.showUploadbtn] 是否显示默认的上传按钮, 默认为不显示
 * @return {zxt.ui.Upload} 根据传入的DOM元素创建文件上传控件对象
 */
zxt.ui.Upload.parse = function(elem) {
	var j = $(elem);
	var _action = j.attr("zxt.action");
	var _count = j.attr("zxt.count");
	var _allowAdd = j.attr("zxt.allowAdd")=="true"?true:false;
	var _showAddbtn = j.attr("zxt.showAddbtn")=="true"?true:false;
	var _showUploadbtn = j.attr("zxt.showUploadbtn")=="true"?true:false;
	
	var _instance = new zxt.ui.Upload(_action,_count,_allowAdd,_showAddbtn,_showUploadbtn);

	return _instance;
};

zxt.defineClazz(zxt.ui.Upload, zxt.ui.Widget);

/**
 * <p>返回控件类型</p>
 * 
 * @function
 * @returns {string} "zxt.ui.Upload"
 */
zxt.ui.Upload.prototype.getType = function() {
	return "zxt.ui.Upload";
};


zxt.ui.Upload.prototype.action = function(v) {
	if(v===undefined){return this._action;}
	this._action = v;
};


/**
 * <p>
 * 渲染该卡文件上传控件的DOM节点
 * </p>
 * 
 * @function
 * @return {DOM} 渲染成的DOM节点
 */
zxt.ui.Upload.prototype.renderSelf = function() {
	var instance = this;
	this._$jqDom.empty();
	this._$form.empty();
	this._$form.append(this._$uploadiframe);
	this._init();
	if(this._showUploadbtn){
		this._$form.append(this._$submit);
	};
	this._$jqDom.append(this._$form);
	return this._$jqDom.get(0);
};

/**
 * <p>文件上传控件初始化</p>
 * @private 
 * @function
 */
zxt.ui.Upload.prototype._init = function(){
	this._createInput(this._$form);
};


/**
 * <p>文件上传控件创建input组</p>
 * @private 
 * @function
 */
zxt.ui.Upload.prototype._createInput = function(form,inputIndex){
	var i=inputIndex||0;     
    if(i==0){     
		this._inputInit(form);
    }     
    else{            
        var inputDiv=$("<div></div>");     
        var input=$("<input>"); 
        input.attr("type","file");     
        input.attr("name","file_"+i);     
        inputDiv.append(input);     
        //inputDiv.append(this._getRemove(form,inputDiv));
        if(this._allowAdd&&this._showAddbtn){
        	if(this._showUploadbtn){
        		var pos = form.get(0).children.length-2;
        	}else{
        		var pos = form.get(0).children.length-1;
        	};
            form.get(0).insertBefore(inputDiv.get(0),form.get(0).children[pos]);
            form.attr("count",i);
        	form.get(0).children[pos+1].innerHTML="再添加一个附件";
        }else{
        	form.append(inputDiv);
        }
    }     
};


/**
 * <p>文件上传控件创建input</p>
 * @private 
 * @function
 */
zxt.ui.Upload.prototype._inputInit = function(form){
	
	if(this._allowAdd&&this._showAddbtn){
		//var tipDiv=document.createElement("div");
		var instance = this;
    	//tipDiv.style.cssText="width:100px;font:12px Arial;color:#00f;text-decoration:underline";     
    	//tipDiv.innerHTML="添加一个附件";     
    	//tipDiv.onclick=function(){     
    		   //instance.addFile();
    	//};     
    	//form.append(tipDiv);
		var addbtn = $("<input type='button' name='add' value='增加附件'>");
		addbtn.click(function(){instance.addFile();});
		form.append(addbtn);
	};
    for(i=0;i<this._count;i++){
    	this._createInput(form,parseInt(i)+1);
    }
};


/**
 * <p>文件上传控件创建删除按钮</p>
 * @private
 * @function
 */
zxt.ui.Upload.prototype._getRemove = function(form,node){
	var deletebtn = $("<input type='button' name='delete' value='清除'>");
	var instance = this;
	deletebtn.click(function(){instance._removeChild(form,node);});
	//var span=document.createElement("span");
	
    //span.style.cssText="font:10px Arial;color:#00f;text-decoration:underline;";     
    //span.innerHTML="移除";     
    //span.onclick=function(){}     
    //return span;
	return deletebtn.get(0);
};


/**
 * <p>文件上传控件删除节点事件</p>
 * @private
 * @function
 */
zxt.ui.Upload.prototype._removeChild = function(parent,child){
	var i=parent.attr("count");
	$(">input", child ).each( function( idx, dom ){
			var jdom = $(dom);
			if(jdom.attr("type")=="file"){
				jdom.val("");
			}
		}
	);
	//child.find("input:file").val(""); 
	//child.remove();
	//i--;     
	if(i==0&&this._allowAdd){
    	parent.get(0).children[1].innerHTML = "添加一个附件";     
  	};     
	parent.attr("count",i);
};


/**
 * <p>文件上传控件提交上传</p>
 * 
 * @function
 */
zxt.ui.Upload.prototype.doUpload = function(){
	this._$form.attr("ACTION",this.action());
	this._$form.get(0).submit();
};


/**
 * <p>文件上传控件增加需要上传的文件</p>
 * 
 * @function
 */
zxt.ui.Upload.prototype.addFile = function(){
	if(this._allowAdd){
		var form = this._$form;
		var i=form.attr("count")||0;     
		this._createInput(form,parseInt(i)+1);
	};
};


/**
 * <p>文件上传控件显示上传成功的文件列表</p>
 * 
 * @function
 */
zxt.ui.Upload.prototype.update = function(){
	var content = this._$uploadiframe.get(0).contentDocument.body;
	var instance = this;
	var count = $(content).find("#fileList").length;
	var remaincount = this._count-count;
	this._count = remaincount>0?remaincount:0;
	this.render();
	$(content).find("#fileList").each(function(){
		instance.$jqDom.before($("<div>"+this.textContent+"上传成功！</div>"));
	});
};


/**
 * <p>文件上传控件工厂类</p>
 * @class 
 */
zxt.ui.UploadFactory = function() {
};

zxt.defineClazz(zxt.ui.UploadFactory, zxt.ui.InteractorFactory);

/**
 * <p>获取相应的文件上传控件实例对象</p>
 * 
 * @function
 * @return {zxt.ui.Upload} 文件上传控件实例对象
 */
zxt.ui.UploadFactory.prototype.getInstance = function() {
	return new zxt.ui.Upload();
};

/**
 * <p>下载控件</p>
 * @class
 * @extends zxt.ui.Widget
 */
zxt.ui.DownLoad = function() {
	zxt.ui.Widget.call(this);
	this._iframeid = zxt.util.GUID();
	this._$downloadiframe = $("<iframe name="+ this._iframeid +" frameBorder='0' style='display:none'></iframe>");
	
	this._$jqDom = $("<div nowrap>");
	
};

/**
 * <p>根据传入的DOM元素, 解析传入的相关属性, 创建相应的文件下载控件对象</p>
 * 
 * @function
 * @static
 * @param {DOM} elem 解析的DOM元素 
 * @return
 */
zxt.ui.DownLoad.parse = function(elem) {
	var _instance = new zxt.ui.DownLoad();
	return _instance;
};

zxt.defineClazz(zxt.ui.DownLoad, zxt.ui.Widget);

/**
 * <p>渲染DOM节点</p>
 * @function
 * @return
 */
zxt.ui.DownLoad.prototype.renderSelf = function(){
	this._$jqDom.empty();
	this._$jqDom.append(this._$downloadiframe);
	return this._$jqDom.get(0);
	
};


/**
 * <p>文件下载控件提交下载</p>
 * @param {zxt.data.DataWrapper} data 数据包
 * @param {URL} location URL 
 * @param {String} page 页面名
 * @param {String} method 回调的方法
 * @return
 */
zxt.ui.DownLoad.prototype.doDownLoad = function(data, location, page, method){
	$zxt.submit(data, location, page, method,this._iframeid);
};































